package com.zhixinsou.wuyang.process;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zhixinsou.wuyang.model.AnalysisReport;
import com.zhixinsou.wuyang.model.SheetModel;
import com.zhixinsou.wuyang.util.FileUtil;
import com.zhixinsou.wuyang.util.StringUtil;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ProcessUBSResearch {
    public static List<AnalysisReport> getAnalysisReportPageInfo(String filePath) {
        List<AnalysisReport> reports = new ArrayList<>();
        int startPage = 1;
        File file = new File(filePath);
        String[] lines = FileUtil.pdfReader(file, startPage, startPage).split("\r\n");
        Pattern pattern = Pattern.compile("([0-9]+)\\s-\\s([0-9]+)$");
        while (lines.length > 1 && lines[0].equals("Table of Contents")) {
            for (String line : lines) {
                Matcher matcher = pattern.matcher(line);
                if (matcher.find()) {
                    AnalysisReport report = new AnalysisReport();
                    report.setFilePath(filePath);
                    report.setFileName(file.getName());
                    report.setStartPage(Integer.parseInt(matcher.group(1)));
                    report.setEndPage(Integer.parseInt(matcher.group(2)));
                    report.setBrokerName("UBS Investment Research");

                    reports.add(report);
                }
            }

            startPage++;
            lines = FileUtil.pdfReader(filePath, startPage, startPage).split("\r\n");
        }
        return reports;
    }

    public static void getAnalysisReportDetail(AnalysisReport report) {
        getAnalysisReportDetail2004_2012_new(report);
    }

    public static void getAnalysisReportDetail2004_2012_new(AnalysisReport report) {
        String content = FileUtil.pdfReader(report.getFilePath(), report.getStartPage(), report.getStartPage(), false);
        String[] lines = content.split("\r\n");

        Pattern patternDate = Pattern.compile("[0-9]+\\s*(January|February|March|April|May|June|July|August|September|October|November|December){1}\\s*[0-9]{4}");
        for (String line : lines) {
            Matcher matcherDate = patternDate.matcher(line.trim());
            if (matcherDate.find()) {
                report.setDate(matcherDate.group());
            }
        }

        Pattern patternRating = Pattern.compile("(Rating|rating){1}\\s(?!to)([A-Z]+[^\\.]+)");
        boolean ratingFound = false;
        for (String line : lines) {
            Matcher matcherRating = patternRating.matcher(line.trim());
            if (matcherRating.find()) {
                report.setRating(matcherRating.group(2));
                ratingFound = true;
                continue;
            }

            if (ratingFound) {
                report.setPriorRecommendation(line.trim());
                break;
            }
        }

        Pattern patternRic = Pattern.compile("RIC:\\s*([a-zA-Z0-9]+\\.[a-zA-Z0-9]+)\\s*.*");
        for (String line : lines) {
            Matcher matcherRic = patternRic.matcher(line.trim());
            if (matcherRic.find()) {
                report.setRic(matcherRic.group(1));
                report.setTicker(matcherRic.group());
                break;
            }
        }

        for (int i = 0; i < lines.length; i++) {
            String line = lines[i];
            if (line.contains("UBS Investment Research")) {
                report.setCompanyName(lines[i + 1]);
            }
        }

        Pattern patternEmail = Pattern.compile("@[a-zA-Z0-9-]+\\.");
        Pattern patternName = Pattern.compile("^([a-zA-Z0-9\\.\\,\\s]+)$");
        for (int i = 0; i < lines.length; i++) {
            String line = lines[i].trim();
            Matcher matcherEmail = patternEmail.matcher(line);
            while (matcherEmail.find()) {
                Matcher matcher = patternName.matcher(lines[i - 2].trim());
                if (matcher.find()) {
                    report.addAnalystName(matcher.group(1));
                } else {
                    matcher = patternName.matcher(lines[i - 1].trim());
                    if (matcher.find()) {
                        report.addAnalystName(matcher.group(1));
                    } else {
                        report.addAnalystName(lines[i - 2].trim() + " - " + lines[i - 1].trim() + " - " + line);
                    }
                } 
            }
        }
        
        /*
        String[] linesWithFonts = FileUtil.pdfReaderWithFonts(report.getFilePath(), report.getStartPage(), report.getStartPage()).split("\r\n");
        boolean authorStart = false;
        boolean authorFound = false;
        Pattern patternEmail = Pattern.compile("@[a-zA-Z0-9]+");
        Pattern patternName = Pattern.compile("\\[ArialNarrow,Bold\\]([^\\[]+)\\[ArialNarrow\\]");
        for(int last = linesWithFonts.length - 1;last > -1;last--) {
            String line = linesWithFonts[last].trim();
            Matcher matcher = patternEmail.matcher(line);
            if (matcher.find()) {
                authorStart = true;
                continue;
            }

            if (authorStart) {
                Matcher matcherName = patternName.matcher(line);
                while (matcherName.find()) {
                    report.addAnalystName(matcherName.group(1));
                    authorFound = true;
                }

                if(authorFound)
                    break;
            }
        }
        */

        content = FileUtil.pdfReader(report.getFilePath(), report.getStartPage(), report.getEndPage());
        lines = content.split("\r\n");
        Pattern patternSentence = Pattern.compile("([0-9a-z]+)\\.\\s*[^\\.]+?covering this company[^\\.]+?\\.");
        Pattern patternTable = Pattern.compile("Company Name\\s*Reuters");
        Matcher matcher = patternSentence.matcher(content);
        boolean tableStart = false;
        while (matcher.find()) {
            String companyIndex = matcher.group(1);
            String sentence = matcher.group().replace("\r\n", "");
            report.addOwnershipDisclosure(sentence);

            for (String line : lines) {
                Matcher matcherTable = patternTable.matcher(line.trim());
                if (!tableStart && matcherTable.find()) {
                    tableStart = true;
                    continue;
                }

                if (tableStart) {
                    Pattern pattern = Pattern.compile("([a-zA-Z]+[0-9]*).*" + companyIndex + "[^a-zA-Z0-9]+.*");
                    Matcher m = pattern.matcher(line);
                    if (m.find()) {
                        report.addCompaniesMentioned(m.group());
                    }
                }

                if(tableStart && line.contains("Source:"))
                    break;
            }
        }

        System.out.println(JSON.toJSON(report));
    }
    
    public static void getAnalysisReportDetail2004_2012(AnalysisReport report) {
        String content = FileUtil.pdfReader(report.getFilePath(), report.getStartPage(), report.getStartPage());
        String[] lines = content.split("\r\n");

        Pattern patternDate = Pattern.compile("[0-9]+\\s*(January|February|March|April|May|June|July|August|September|October|November|December){1}\\s*[0-9]{4}");
        for (String line : lines) {
            Matcher matcherDate = patternDate.matcher(line);
            if (matcherDate.find()) {
                report.setDate(matcherDate.group());
            }
        }
        
        //去除空行
        int index = 0;
        for(; index < lines.length; index++) {
            if(StringUtil.isEmpty(lines[index].trim()))
                continue;
            else
                break;
        }

        Pattern patternRating = Pattern.compile("Rating\\s(.+)");
        for(; index < lines.length; index++) {
            Matcher matcherRating = patternRating.matcher(lines[index].trim());
            if (matcherRating.find()) {
                report.setRating(matcherRating.group(1));
                index++;
                break;
            }
        }

        //去除空行
        for(; index < lines.length; index++) {
            if(StringUtil.isEmpty(lines[index].trim()))
                continue;
            else
                break;
        }
        
        Pattern patternCompany = Pattern.compile("([a-zA-Z]+)$");
        String companyLine = lines[index++].trim();
        Matcher matcherCompany = patternCompany.matcher(companyLine);
        if (matcherCompany.find()) {
            report.setPriorRecommendation(matcherCompany.group(1));
            report.setCompanyName(companyLine.replace(report.getPriorRecommendation(), "").trim());
        } else {
            report.setCompanyName(companyLine);
        }

        //去除空行
        for(; index < lines.length; index++) {
            if(StringUtil.isEmpty(lines[index].trim()))
                continue;
            else
                break;
        }

        Pattern patternRic = Pattern.compile("RIC:\\s*([a-zA-Z0-9]+\\.[a-zA-Z0-9]+)\\s*.*");
        for(; index < lines.length; index++) {
            Matcher matcherRic = patternRic.matcher(lines[index].trim());
            if (matcherRic.find()) {
                report.setRic(matcherRic.group(1));
                report.setTicker(matcherRic.group());
                index++;
                break;
            }
        }

        String[] linesWithFonts = FileUtil.pdfReaderWithFonts(report.getFilePath(), report.getStartPage(), report.getStartPage()).split("\r\n");
        boolean authorStart = false;
        boolean authorFound = false;
        Pattern patternEmail = Pattern.compile("@[a-zA-Z0-9]+");
        Pattern patternName = Pattern.compile("\\[ArialNarrow,Bold\\]([^\\[]+)\\[ArialNarrow\\]");
        for(int last = linesWithFonts.length - 1;last > -1;last--) {
            String line = linesWithFonts[last].trim();
            Matcher matcher = patternEmail.matcher(line);
            if (matcher.find()) {
                authorStart = true;
                continue;
            }

            if (authorStart) {
                Matcher matcherName = patternName.matcher(line);
                while (matcherName.find()) {
                    report.addAnalystName(matcherName.group(1));
                    authorFound = true;
                }

                if(authorFound)
                    break;
            }
        }
        
        //去除空行
        for(; index < lines.length; index++) {
            if(StringUtil.isEmpty(lines[index].trim()))
                continue;
            else
                break;
        }

        content = FileUtil.pdfReader(report.getFilePath(), report.getStartPage(), report.getEndPage());
        Pattern patternSentence = Pattern.compile("([0-9a-z]+)\\.\\s*[^\\.]+?covering this company[^\\.]+?\\.");
        Pattern patternTable = Pattern.compile("Company Name\\s*Reuters");
        Matcher matcher = patternSentence.matcher(content);
        boolean tableStart = false;
        while (matcher.find()) {
            String companyIndex = matcher.group(1);
            String sentence = matcher.group().replace("\r\n", "");
            report.addOwnershipDisclosure(sentence);

            for (String line : lines) {
                Matcher matcherTable = patternTable.matcher(line.trim());
                if (!tableStart && matcherTable.find()) {
                    tableStart = true;
                    continue;
                }

                if (tableStart) {
                    Pattern pattern = Pattern.compile("([a-zA-Z0-9]+).*" + companyIndex + "[^a-zA-Z0-9]+.*");
                    Matcher m = pattern.matcher(line);
                    if (m.find()) {
                        report.addCompaniesMentioned(m.group());
                    }
                }

                if(tableStart && line.contains("Source:"))
                    break;
            }
        }

        System.out.println(JSON.toJSON(report));
    }

    public static void process(String rootDir, String resultPath) {
        List<AnalysisReport> reports = new ArrayList<>();
        File[] Files = new File(rootDir).listFiles();
        for (File file : Files) {
            if(file.getName().endsWith(".pdf"))
                reports.addAll(getAnalysisReportPageInfo(file.getPath()));
        }

        //以下代码为写入excel
        List<String> headers = new ArrayList<>();
        headers.add("Date");
        headers.add("Broker name");
        headers.add("Analyst name");
        headers.add("Company name");
        headers.add("Ticker");
        headers.add("Exchange");
        headers.add("RIC");
        headers.add("Rating");
        headers.add("Initiating Coverage");
        headers.add("Prior recommendation");
        headers.add("Ownership disclosure");
        headers.add("Companies Mentioned");
        headers.add("PDF file name");
        headers.add("StartPage");
        headers.add("EndPage");
        SheetModel sheetModel = FileUtil.makeSheet(resultPath, "result", headers);

        List<String[]> data = new ArrayList<>();
        for (AnalysisReport report : reports) {
            try {
                getAnalysisReportDetail(report);
            } catch (Exception e) {
                System.out.println(JSON.toJSON(report));
                e.printStackTrace();
                continue;
            }
//            String sentence;
//            if(report.getOwnershipDisclosures() != null && report.getOwnershipDisclosures().size() == 1)
//                sentence = report.getOwnershipDisclosures().get(0);
//            else
//                sentence = JSON.toJSONString(report.getOwnershipDisclosures());

            String[] line = new String[]{report.getDate(),
                    report.getBrokerName(),
                    JSON.toJSONString(report.getAnalystNames()),
                    report.getCompanyName(),
                    report.getTicker(),
                    report.getExchange(),
                    report.getRic(),
                    report.getRating(),
                    report.getInitiatingCoverage(),
                    report.getPriorRecommendation(),
                    JSON.toJSONString(report.getOwnershipDisclosures()),
                    JSON.toJSONString(report.getCompaniesMentioned()),
                    report.getFileName(),
                    report.getStartPage() + "",
                    report.getEndPage() + ""};
            data.add(line);
        }

        FileUtil.exportSheet(sheetModel.getSheet(), data);
        FileUtil.closeSheet(sheetModel);
        System.out.println("Success!");
    }
}
